package com.hbusa.dao.impl;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.hbusa.dao.CategoryDao;
import com.hbusa.dto.TbCategory;
import lombok.val;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static cn.hutool.db.ActiveEntity.create;

//https://blog.csdn.net/waiter_chen/article/details/118329808
public class CategoryDaoImpl implements CategoryDao {

    @Override
    public boolean add(TbCategory category) throws SQLException {
        int insert = Db.use().insert(
                create("tb_category")
                        .set("cat_name", category.getCatName())
        );
        return insert > 0;
    }
    @Override
    public boolean removeById(long cat_id) throws SQLException {
        int del = Db.use().del(
                Entity.create("tb_category").set("cat_id", cat_id)
        );
        return del > 0;
    }

    @Override
    public boolean modify(long cat_id, TbCategory category) throws SQLException {

        int rows = Db.use().update(
                Entity.create("tb_category").set("cat_name",category.getCatName()),
                Entity.create("tb_category").set("cat_id",cat_id)
                );
        return rows > 0;
    }



    @Override
    public ArrayList<TbCategory> findAll() throws SQLException {
        ArrayList<TbCategory> arrayList = new ArrayList<>();
        List<Entity> all = Db.use().findAll("tb_category");
//        Entity entity = all.get(0);
//        TbCategory tbCategory = null;
        for(Entity entity1:all){
            TbCategory category = new TbCategory(
                    (int)entity1.get("cat_id"),
                    (String)entity1.get("cat_name")
            );
//            entity1 = new Entity((String) entity1.get("cat_name"));
//            entity1 = new Entity((String) entity1.get("cat_id"));
            arrayList.add(category);
        }

        return arrayList;
    }

    public TbCategory findById(int cat_id) throws SQLException {
        List<Entity> all = Db.use().findAll(Entity.create("tb_category").set("cat_id", cat_id));
//        System.out.println(all);
        Entity entity = all.get(0);
//        System.out.println(entity);



        TbCategory category = null;
        if (all != null && all.size() > 0) {
            category = new TbCategory();
            category.setCatId((Integer) entity.get("cat_id"));
            category.setCatName((String)entity.get("cat_name"));
        }
        return category;
    }

    public static void main(String[] args) throws SQLException {
        val categoryDao = new CategoryDaoImpl();
        TbCategory category  = null;

        /*
        找一个
        category = categoryDao.findById(1);
        System.out.println(category);
         */
        /*
        找全部
        ArrayList<TbCategory> all = categoryDao.findAll();
        for (TbCategory tbCategory : all) {
            System.out.println(tbCategory);
        }
        */
//        category.setCatName("zx");
//        添加
        category = new TbCategory();

        boolean add = categoryDao.add(category);
        System.out.println(add);

        /*
        修改
        boolean modify = categoryDao.modify(1, category);
         */

//        boolean del = categoryDao.removeById(1);

//        System.out.println(findById);
//        if (add) {
//            System.out.println("++++++++成功");
//        } else {
//            System.out.println("++++++++失败");
//        }
//
    }
}
